﻿using System;
using System.Collections.Generic;
using System.Threading;
using QFramework;
using UnityEngine;

namespace ProjectMyFarmAndShe
{
    public class EventController : IDisposable
    {
        private static EventController mInstance = new EventController();

        public static EventController Default => mInstance;
        
        private EventController()
        {
        }

        private Dictionary<string, EasyEvent> mEventDic = new Dictionary<string, EasyEvent>();

        public void AddEvent(string key, EasyEvent easyEvent)
        {
            mEventDic.TryAdd(key, easyEvent);
        }

        public EasyEvent GetEvent(string key)
        {
            if (!mEventDic.ContainsKey(key))
            {
                return null;
            }
            
            return mEventDic[key];
        }

        public void TriggerEvent(string key)
        {
            if (!mEventDic.ContainsKey(key))
            {
                return;
            }
            
            mEventDic[key].Trigger();
        }

        public void Dispose()
        {
            mInstance = null;
            mEventDic.Clear();
        }
    }
}